require "import"
import "str"

--pid=...

--rc4=import "mods.minicrypto"

--import "android.support.v4.app.*"
--import "android.support.v4.view.*"
import "android.support.v4.widget.*"

topbar_click=0

local function isVip()
return activity.getSharedData("vip_time")~=nil and activity.getSharedData("vip_time")~="已到期" and activity.getSharedData("vip_time")~="未开通"
end

this.setContentView(loadlayout ({
  RelativeLayout,
  --layout_width="fill",
  --layout_height="fill",
  --visibility=4,
  backgroundColor=背景色,
  --orientation="vertical",
  --elevation="2%w",
  paddingTop=状态栏高度,
  {
    RelativeLayout,
    layout_width="fill",
    layout_height="56dp" ,
    gravity="center",
    padding="16dp",
    --backgroundColor=0xffffffff,
    elevation="2dp",
    paddingTop="8dp",
    paddingBottom="8dp",
    onClick=function()
      if topbar_click+2 > tonumber(os.time()) then
        ObjectAnimator.ofInt(loadb, "scrollY", {loadb.scrollY,0})
        .setDuration(256)
        .setInterpolator(luajava.newInstance("android.view.animation.DecelerateInterpolator"))
        .start()
       else
        topbar_click=tonumber(os.time())
      end
    end;
    {
      ImageView,
      src="drawable/back.png",
      layout_height="fill",
      layout_width="44dp",
      foreground=波纹(波纹色),
      onClick=function ()
        this.finish()
      end,
      padding="9dp",
      colorFilter=图标色,
    },
    {
      ImageView,
      src="drawable/dots.png",
      layout_height="fill",
      layout_width="44dp",
      foreground=波纹(波纹色),
      padding="9dp",
      colorFilter=图标色,
      layout_alignParentRight=true,
      onClick=function ()
        对话框操作()
      end,
    },
    {
      TextView,
      layout_width="fill",
      layout_height="fill",
      gravity="center",
      --layout_gravity="center",
      text="古诗词",
      textSize="20sp",
      textColor=文字色,
    },
  },
  {
    SwipeRefreshLayout;
    id="swipe1";
    layout_height="-1";
    layout_width="-1";
    {
      ScrollView,
      layout_height="-1";
      layout_width="-1";
      id="loadb";
      {
        LinearLayout,
        layout_width="fill",
        layout_height="-1";
        orientation="vertical",
        {
          LinearLayout,
          paddingTop="56dp" ,
        };
        {
          LinearLayout;
          orientation="vertical";
          layout_height="-1";
          layout_width="-1";
          gravity=17,
       
          {
            LinearLayout;
            orientation="vertical";
            layout_height="-1";
            gravity=17,
            layout_width="-1";
            padding="16dp";
            {
              TextView;
              --layout_width="-1";
              --layout_height="-2";
              gravity="center";
              textSize="14sp";
              textColor=次要文字色;
              background="#10000000";
              layout_marginBottom="16dp";
              text="长按标题收藏本篇，下滑刷新内容（点击此消息不再提示）";
              padding="8dp";
              id="czts";
            };
            {
            LinearLayout,
            layout_marginBottom="16dp",
            gravity=17,
            padding="16dp",
            onClick=function(v)
            if activity.getSharedData("password")==nil then
            toast("此功能需要会员，请先登录")
            activity.newActivity("login",{"sup"})
            elseif !isVip() then
            toast("此功能需要会员")
            activity.newActivity("sup")
            else
            v=v.getChildAt(1)
            local pop=PopupMenu(this,v)
        local menu=pop.Menu
        local function setText(a)
          v.setText(tostring(a))
          reLoadText()
        end
        menu.add("全部").onMenuItemClick=setText
        menu.add("抒情").onMenuItemClick=setText
        menu.add("四季").onMenuItemClick=setText
        menu.add("山水").onMenuItemClick=setText
        menu.add("天气").onMenuItemClick=setText
        menu.add("人物").onMenuItemClick=setText
        menu.add("人生").onMenuItemClick=setText
        menu.add("生活").onMenuItemClick=setText
        menu.add("节日").onMenuItemClick=setText
        menu.add("动物").onMenuItemClick=setText
        menu.add("植物").onMenuItemClick=setText
        menu.add("食物").onMenuItemClick=setText
        pop.show()
            end
            end,
            foreground=波纹(波纹色),
            {
            TextView,
            text="分类",
            textSize="16sp",
            textColor=文字色,
            layout_marginRight="12dp",
            
            },
            {
            TextView,
            text="全部",
            id="cateSpin",
            textSize="16sp",
            textColor=文字色,
            },
            },
            {
              TextView;
              --layout_width="-1";
              --layout_height="-2";
              gravity="center";
              textSize="20sp";
              textColor=文字色;
              id="today_title";
              layout_marginBottom="12dp";
            };
            {
              TextView;
              --layout_width="-1";
              --layout_height="-2";
              gravity="center";
              textSize="14sp";
              textColor=次要文字色;
              id="today_author";
              layout_marginBottom="16dp";
            };
            {
              TextView;
              ---layout_width="-1";
              --layout_height="-2";
              textSize="16sp";
              textColor=文字色;
              id="today_text";
              gravity="center";
              layout_marginBottom="16dp";
            };
          };
        };
      };
      --scroll
    },
  },
  {
    LinearLayout,
    layout_width="fill",
    layout_height="56dp" ,
    foreground=上下渐变({背景色,深透,淡透}),
  },
}))

function 对话框操作()
  local edt=AlertDialog.Builder(this)
  -- .setCancelable(false)
  .setTitle("选择操作")
  .setItems({"添加收藏","分享内容","复制内容"}, function (d,n)
    local hiscon=today_title.Text.."\n    "..today_author.text.."\n    "..today_text.Text
    if n==1 then
      if utf8.len(hiscon)<=100 then
        texta=utf8.sub(hiscon,1,utf8.len(hiscon)-1).."……"
       else
        texta=utf8.sub(hiscon,1,100).."……"
      end

      textn=texta.."\n（复制此消息，打开闲言APP阅读全文）\n&MS"..tostring(now_content).."&\nhttp://m.pp.cn/detail.html?appid=8050563&ch_src=pp_dev&ch=default\n——闲言APP "..slogan
      shareText(textn)
     elseif n==2 then
      copyText(hiscon.."\n    ——闲言APP "..slogan)
     elseif n==0 then
      io.open(句子收藏..os.time().."("..#hiscon..")","w+"):write([[{
soup=]].."[["..hiscon.."]]"..[[,
type=]].."[[微风暴]]"..[[,
}]]):close()
      toast ("已加收藏，可在灵感中管理")
    end
  end)
  local ed=edt.show()
  local pw=ed.getWindow()
  --.setWindowAnimations(R.style.BottomDialog_Animation)
  --.setBackgroundDrawable(ColorDrawable(0))
  .setGravity(Gravity.BOTTOM)
  圆角(pw,背景色,{0,0,0,0,0,0,0,0})
  local lp=pw.getAttributes()
  lp.width=w
  pw.setAttributes(lp)
  pw.setDimAmount(0.35)
end



local aid={swipe1}
for i=1,#aid do
  aid[i].setProgressViewOffset(true,dp2px(56),dp2px(32+56))
  aid[i].setColorSchemeColors({文字色})
  aid[i].setProgressBackgroundColorSchemeColor(背景色)
end
aid=nil

today_text.setLineSpacing(48,0.5)



--math.randomseed(os.time())
--math.random() -- 初始化随机数 不然容易导致第一个数不随机


function reLoadText()
  if isVip() then
    --"https://www.lzs100.cc/post/%s.html"
    
    local cate=({
      全部="all",
      抒情="shuqing",
      四季="siji",
      山水="shanshui",
      天气="tianqi",
      人物="renwu",
      人生="rensheng",
      生活="shenghuo",
      节日="jieri",
      动物="dongwu",
      植物="zhiwu",
      食物="shiwu",
    })[cateSpin.text]
    
    local url = "https://v1.jinrishici.com/"..cate

    Http.get(url, function(code, body)
      swipe1.setRefreshing(false)
      if code == 200 then
        local nbody
        
        pcall(function ()
          nbody=cjson.decode(body)--body:match([[neir">(.-)<div]])
        end)
        
        if !nbody then
          toast("内容获取错误")
          return
        end
        
        today_title.setText(nbody.origin)
        today_author.setText(nbody.author)
        today_text.setText(nbody.content)

       else
        toast("网络错误:"..code)

      end
    end)

   else
toast("此功能需要会员啦")
    swipe1.setRefreshing(false)

   today_text.setText("          闲言App从上线以为一直坚持绿色轻量的作风，以节省成本为要、用户体验为先\n         无广告成为最大的特点！\n        近期用户增加，服务器承载能力减弱，维护成本较高，考虑到节省成本，部分功能须会员才能使用，感谢支持  \n        闲言会员签到即可领取，只需每天签到就可保持会员状态！\n     您还不是闲言会员，登录后签到领取会员会体验最新功能，您也可以加入闲言官方QQ群，获取会员体验资格，\n首次加载时间较长，请耐心等待...")
    
  end
end




swipe1.setRefreshing(true)
reLoadText()

swipe1.setOnRefreshListener{onRefresh=function()
    swipe1.setRefreshing(true)
    reLoadText()
  end}

now_content=1

today_title.onLongClick=function()
  对话框操作()
end

czts.onClick=function()
  czts.setVisibility(8)
  activity.setSharedData("microstormts","true")
end

if activity.getSharedData("microstormts")=="true" then
  czts.setVisibility(8)
end